/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xmessage_impl.h
 *
 * Purpose: message base implementation
 *
 * Developer:
 *   wen.gu , 2023-10-26
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_COM_XMESSAGE_IMPL_H__
#define __XBUS_COM_XMESSAGE_IMPL_H__


#include "xbus/com/xmessage.h"


namespace xbus::com {


class XMessageImpl: public XMessage {
public:
    virtual ~XMessageImpl();

protected:
    XMessageImpl(uint8_t* buf);

public: /** [mandatory] base header operation */
    uint8_t* data() override { return msg_data_; }
    uint32_t length() const override { return internal::ByteRead4(msg_data_); }
    uint16_t start_code() const override { return internal::ByteRead2(msg_data_ + XMSG_FIELD_OFFSET_STARTCODE); }
    uint8_t flags() const override { return msg_data_[XMSG_FIELD_OFFSET_FLAGS]; }
    uint8_t protocol_version() const override { return XMSG_GET_PROTOCOL_VER(flags()); }
    uint8_t type() const override { return msg_data_[XMSG_FIELD_OFFSET_TYPE]; }
    MessageId message_id() const override { return internal::ByteRead2(msg_data_ + XMSG_FIELD_OFFSET_MSG_ID); }
    SequenceNumber seq_num() const override { return internal::ByteRead2(msg_data_ + XMSG_FIELD_OFFSET_SESSION_ID);}

    void set_type(uint8_t type) override { msg_data_[XMSG_FIELD_OFFSET_TYPE] = type; }
    void set_message_id(MessageId message_id) override { internal::ByteWrite2(msg_data_ + XMSG_FIELD_OFFSET_MSG_ID, message_id); }
    void set_seq_num(SequenceNumber seq_num) override { internal::ByteWrite2(msg_data_ + XMSG_FIELD_OFFSET_SESSION_ID, seq_num); }

public: /** [optional] packet information operation, multi packet info for a big message (extension header) */
    bool is_tp_message() const override { return XMSG_FLAGS_TP_FLAG(flags()); }
    uint32_t pkt_total() const override { return is_tp_message() ? internal::ByteRead2(msg_data_ + XMSG_FIELD_OFFSET_PKT_TOTAL) : 0; }
    uint32_t pkt_idx() const override { return is_tp_message() ? internal::ByteRead2(msg_data_ + XMSG_FIELD_OFFSET_PKT_IDX) : XMSG_PKT_IDX_INVALID; }

    void set_pkt_total(uint32_t total) override { if (is_tp_message()) { internal::ByteWrite2(msg_data_ + XMSG_FIELD_OFFSET_PKT_TOTAL, total);} }
    void set_pkt_idx(uint32_t idx) override { if (is_tp_message()) { internal::ByteWrite2(msg_data_ + XMSG_FIELD_OFFSET_PKT_IDX, idx);} }

public: /** payload operation */
    uint8_t* payload() const override { return msg_data_ + XMessage::GetHeaderLength(flags()); }
    uint32_t payload_size() const override { return length() - XMessage::GetHeaderLength(flags()); }

    XMessageList unpack(uint32_t pkt_size) override;
public: /** Create XMessage */
    /**
     * payload: 
     * if != nullptr, then Create XMessage and copy payload data.
     * if == nullptr, just Create XMessage with 'payload_size'
    */
    static XMessageSp CreateMsg(uint32_t payload_size, const uint8_t* payload = nullptr);
    static XMessageSp CreateTpMsg(uint32_t payload_size, const uint8_t* payload = nullptr); 

    /**byte buffer data to 'XMessage' structure,
     * msg_data: will relase by XMessage destruction function
     */
    static XMessageSp BuildMsg(uint8_t* msg_data, uint32_t msg_length);

    static XMessageSp CopyMsg(const uint8_t* msg_data, uint32_t msg_length);

private:
    uint8_t* msg_data_ = nullptr;
};



} //namespace xbus::com


#endif /** !__XBUS_COM_XMESSAGE_IMPL_H__ */